package service.impl;

import java.util.List;

import org.springframework.dao.DataAccessException;

import persistence.dao.TdfkfaAuthorityDAO;
import persistence.dao.TdfkfaUaDAO;
import persistence.dao.TdfkfaUserDAO;
import persistence.model.TdfkfaAuthority;
import persistence.model.TdfkfaAuthorityExample;
import persistence.model.TdfkfaUa;
import persistence.model.TdfkfaUser;
import persistence.model.TdfkfaUserExample;
import persistence.model.TdfkfaUserExample.Criteria;
import service.UserService;
import util.PasswordUtil;
import util.StringUtil;

public class UserServiceImpl implements UserService {

    private TdfkfaUserDAO tdfkfaUserDAO;

    private TdfkfaAuthorityDAO tdfkfaAuthorityDAO;

    private TdfkfaUaDAO tdfkfaUaDAO;

    public String generatePlanUserId() {
        return "ABC";
    }

    public String generatePassword(int length) {
        return PasswordUtil.getRandomPassword(length);
    }

    public String savePlanUser(TdfkfaUser planUser) {

        try {
        	TdfkfaUserExample uExample = new TdfkfaUserExample();
            uExample.createCriteria().andUsernameEqualTo(planUser.getUsername()).andEnabledEqualTo(new Short("1"));
            List<TdfkfaUser> oUserList = tdfkfaUserDAO.selectByExample(uExample);

            if (oUserList.size() > 0) {
            	return "same username";
            }

            tdfkfaUserDAO.insertSelective(planUser);

            List<TdfkfaUser> userList = tdfkfaUserDAO.selectByExample(uExample);

            TdfkfaUa ua = new TdfkfaUa();
            ua.setUid(userList.get(0).getUid());

            TdfkfaAuthorityExample aExample = new TdfkfaAuthorityExample();
            aExample.createCriteria().andAuthorityEqualTo("ROLE_PLANUSER");
            List<TdfkfaAuthority> aList = tdfkfaAuthorityDAO.selectByExample(aExample);

            ua.setAid(aList.get(0).getAid());

            tdfkfaUaDAO.insert(ua);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }
    
    public String saveExpertUser(TdfkfaUser expertUser) {

        try {
        	TdfkfaUserExample uExample = new TdfkfaUserExample();
            uExample.createCriteria().andUsernameEqualTo(expertUser.getUsername()).andEnabledEqualTo(new Short("1"));
            List<TdfkfaUser> oUserList = tdfkfaUserDAO.selectByExample(uExample);

            if (oUserList.size() > 0) {
            	return "same username";
            }

            tdfkfaUserDAO.insertSelective(expertUser);

            List<TdfkfaUser> userList = tdfkfaUserDAO.selectByExample(uExample);

            TdfkfaUa ua = new TdfkfaUa();
            ua.setUid(userList.get(0).getUid());

            TdfkfaAuthorityExample aExample = new TdfkfaAuthorityExample();
            aExample.createCriteria().andAuthorityEqualTo("ROLE_EXPERTUSER");
            List<TdfkfaAuthority> aList = tdfkfaAuthorityDAO.selectByExample(aExample);

            ua.setAid(aList.get(0).getAid());

            tdfkfaUaDAO.insert(ua);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    public String changePassword(String userName, String password) {
        TdfkfaUser record = new TdfkfaUser();
        record.setPassword(password);
        TdfkfaUserExample example = new TdfkfaUserExample();
        example.createCriteria().andUsernameEqualTo(userName);
        int ret = tdfkfaUserDAO.updateByExampleSelective(record, example);
        if (ret > 0) {
            return "success";
        } else {
            return "error";
        }
    }

    public TdfkfaUser getUserByUsername(String userName) {
        TdfkfaUserExample example = new TdfkfaUserExample();
        example.createCriteria().andUsernameEqualTo(userName);
        List<TdfkfaUser> list = tdfkfaUserDAO.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public int savePlanToUser(String userName, Integer planId, String planName) {
        TdfkfaUserExample example = new TdfkfaUserExample();
        example.createCriteria().andUsernameEqualTo(userName);
        TdfkfaUser record = new TdfkfaUser();
        record.setPlanId(planId);
        record.setPlanName(planName);
        return tdfkfaUserDAO.updateByExampleSelective(record, example);
    }
    
    public void setTdfkfaUserDAO(TdfkfaUserDAO tdfkfaUserDAO) {
        this.tdfkfaUserDAO = tdfkfaUserDAO;
    }

    public void setTdfkfaAuthorityDAO(TdfkfaAuthorityDAO tdfkfaAuthorityDAO) {
        this.tdfkfaAuthorityDAO = tdfkfaAuthorityDAO;
    }

    public void setTdfkfaUaDAO(TdfkfaUaDAO tdfkfaUaDAO) {
        this.tdfkfaUaDAO = tdfkfaUaDAO;
    }

	public int countUser() {
		TdfkfaUserExample example = new TdfkfaUserExample();
		return tdfkfaUserDAO.countByExample(example);
	}

	public void deleteUser(Integer[] ids) {
		TdfkfaUserExample example = new TdfkfaUserExample();
		if (ids.length != 0) {
			for (Integer id : ids) {
				Criteria criteria = example.createCriteria().andUidEqualTo(id);
				example.or(criteria);
			}
			tdfkfaUserDAO.deleteByExample(example);
		}
	}

	public TdfkfaUser getUser(Integer userId) {
		return tdfkfaUserDAO.selectByPrimaryKey(userId);
	}

	public List<TdfkfaUser> getUserList(Integer unitId, int pageIndex, int pageSize) {
		TdfkfaUserExample example = new TdfkfaUserExample();
		if (unitId != null) {
			example.createCriteria().andUnitIdEqualTo(unitId);
		}
		example.setOrderByClause("create_date desc");
		List<TdfkfaUser> list = tdfkfaUserDAO.selectByExample(example, pageIndex, pageSize);
		return list;
	}

	public List<TdfkfaUser> getUserListByAuthority(Integer auth, int pageIndex, int pageSize) {
        List<TdfkfaUser> list = tdfkfaUserDAO.selectUserByAuthority(auth, pageIndex, pageSize);
        return list;
    }
	
	public int countUserByAuthority(Integer auth) {
		int count = tdfkfaUserDAO.countUserByAuthority(auth);
        return count;
    }

	public int updateUser(TdfkfaUser user) {
		if (StringUtil.isEmpty(user.getPassword())) {
			user.setPassword(null);
		}
		int ret = tdfkfaUserDAO.updateByPrimaryKeySelective(user);
		return ret;
	}

}
